<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
<meta name="viewport"
      content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">

    <meta name="author" content="ZhangPing">





<title>JavaScript--es6 | ZhangPing&#39;s Blog</title>



    <link rel="icon" href="/favicon.ico">




    <!-- stylesheets list from _config.yml -->
    
    <link rel="stylesheet" href="/css/style.css">
    



    <!-- scripts list from _config.yml -->
    
    <script src="/js/script.js"></script>
    
    <script src="/js/tocbot.min.js"></script>
    



    
    
        
    


<meta name="generator" content="Hexo 4.2.0"></head>
<body>
    <div class="wrapper">
        <header>
    <nav class="navbar">
        <div class="container">
            <div class="navbar-header header-logo"><a href="/">ZhangPing&#39;s Blog</a></div>
            <div class="menu navbar-right">
                
                    <a class="menu-item" href="/archives">Posts</a>
                
                    <a class="menu-item" href="/category">Categories</a>
                
                    <a class="menu-item" href="/tag">Tags</a>
                
                    <a class="menu-item" href="/about">About</a>
                
                <input id="switch_default" type="checkbox" class="switch_default">
                <label for="switch_default" class="toggleBtn"></label>
            </div>
        </div>
    </nav>

    
    <nav class="navbar-mobile" id="nav-mobile">
        <div class="container">
            <div class="navbar-header">
                <div>
                    <a href="/">ZhangPing&#39;s Blog</a><a id="mobile-toggle-theme">·&nbsp;Light</a>
                </div>
                <div class="menu-toggle" onclick="mobileBtn()">&#9776; Menu</div>
            </div>
            <div class="menu" id="mobile-menu">
                
                    <a class="menu-item" href="/archives">Posts</a>
                
                    <a class="menu-item" href="/category">Categories</a>
                
                    <a class="menu-item" href="/tag">Tags</a>
                
                    <a class="menu-item" href="/about">About</a>
                
            </div>
        </div>
    </nav>

</header>
<script>
    var mobileBtn = function f() {
        var toggleMenu = document.getElementsByClassName("menu-toggle")[0];
        var mobileMenu = document.getElementById("mobile-menu");
        if(toggleMenu.classList.contains("active")){
           toggleMenu.classList.remove("active")
            mobileMenu.classList.remove("active")
        }else{
            toggleMenu.classList.add("active")
            mobileMenu.classList.add("active")
        }
    }
</script>
        <div class="main">
            <div class="container">
    
    
        <div class="post-toc">
    <div class="tocbot-list">
    </div>
    <div class="tocbot-list-menu">
        <a class="tocbot-toc-expand" onclick="expand_toc()">Expand all</a>
        <a onclick="go_top()">Back to top</a>
        <a onclick="go_bottom()">Go to bottom</a>
    </div>
</div>

<script>
    document.ready(
        function () {
            tocbot.init({
                tocSelector: '.tocbot-list',
                contentSelector: '.post-content',
                headingSelector: 'h1, h2, h3, h4, h5',
                collapseDepth: 1,
                orderedList: false,
                scrollSmooth: true,
            })
        }
    )

    function expand_toc() {
        var b = document.querySelector(".tocbot-toc-expand");
        tocbot.init({
            tocSelector: '.tocbot-list',
            contentSelector: '.post-content',
            headingSelector: 'h1, h2, h3, h4, h5',
            collapseDepth: 6,
            orderedList: false,
            scrollSmooth: true,
        });
        b.setAttribute("onclick", "collapse_toc()");
        b.innerHTML = "Collapse all"
    }

    function collapse_toc() {
        var b = document.querySelector(".tocbot-toc-expand");
        tocbot.init({
            tocSelector: '.tocbot-list',
            contentSelector: '.post-content',
            headingSelector: 'h1, h2, h3, h4, h5',
            collapseDepth: 1,
            orderedList: false,
            scrollSmooth: true,
        });
        b.setAttribute("onclick", "expand_toc()");
        b.innerHTML = "Expand all"
    }

    function go_top() {
        window.scrollTo(0, 0);
    }

    function go_bottom() {
        window.scrollTo(0, document.body.scrollHeight);
    }

</script>
    

    
    <article class="post-wrap">
        <header class="post-header">
            <h1 class="post-title">JavaScript--es6</h1>
            
                <div class="post-meta">
                    
                        Author: <a itemprop="author" rel="author" href="/">ZhangPing</a>
                    

                    
                        <span class="post-time">
                        Date: <a href="#">December 12, 2017&nbsp;&nbsp;19:00:00</a>
                        </span>
                    
                    
                        <span class="post-category">
                    Category:
                            
                                <a href="/categories/JavaScript/">JavaScript</a>
                            
                        </span>
                    
                </div>
            
        </header>

        <div class="post-content">
            <h3 id="1-块作用域-let"><a href="#1-块作用域-let" class="headerlink" title="1.块作用域-let"></a>1.块作用域-let</h3><p>使用let定义的变量，只能在其所在的代码块中使用。</p>
<p>定义一个块，最简单的方法就是使用一个大括号{}。在{}里面使用let定义的变量，在{}外面是访问不到的。</p>
<h3 id="2-恒量-const"><a href="#2-恒量-const" class="headerlink" title="2.恒量-const"></a>2.恒量-const</h3><p>使用const定义的“变量”一旦声明赋值，就不能对其再次赋值。</p>
<p>const限制的是分配值的动作，并不是限制值的内容。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//定义一个数组</span></span><br><span class="line"><span class="keyword">const</span> people = [];</span><br><span class="line">people.push(<span class="string">'zhang san'</span>);</span><br><span class="line">people.push(<span class="string">'lisi'</span>);</span><br><span class="line">console.log(people);</span><br><span class="line"><span class="comment">//控制台输出：["zhangsan","lisi"]</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//将people重新赋值</span></span><br><span class="line">people = [];</span><br><span class="line">console.log(people);</span><br><span class="line"><span class="comment">//控制台报错</span></span><br></pre></td></tr></table></figure>

<h3 id="3-解构"><a href="#3-解构" class="headerlink" title="3.解构"></a>3.解构</h3><p>解构就是分解一个东西的结构。</p>
<h4 id="解构数组-array-Destructuring"><a href="#解构数组-array-Destructuring" class="headerlink" title="解构数组-array Destructuring"></a>解构数组-array Destructuring</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//定义一个方法，返回一个数组</span></span><br><span class="line"><span class="function">function <span class="title">breakfast</span><span class="params">()</span></span>&#123;</span><br><span class="line"> <span class="keyword">return</span> [<span class="string">'cake'</span>,<span class="string">'tea'</span>,<span class="string">'apple'</span>];</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//es5写法</span></span><br><span class="line"><span class="keyword">var</span> tmp = breakfast(),dessert=tmp[<span class="number">0</span>],drink=tmp[<span class="number">1</span>],fruit=tmp[<span class="number">2</span>];</span><br><span class="line"></span><br><span class="line"><span class="comment">//es6</span></span><br><span class="line">let [dessert,drink,fruit] = breakfast();</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">console.log(dessert,drink,fruit);</span><br><span class="line"><span class="comment">//控制台输出：cake tea apple</span></span><br></pre></td></tr></table></figure>

<h4 id="解构对象-object-Destructing"><a href="#解构对象-object-Destructing" class="headerlink" title="解构对象-object Destructing"></a>解构对象-object Destructing</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//定义一个方法，返回一个对象</span></span><br><span class="line"><span class="function">function <span class="title">breakfast</span><span class="params">()</span></span>&#123;</span><br><span class="line"> <span class="keyword">return</span> &#123;dessert:<span class="string">'cake'</span>,drink:<span class="string">'tea'</span>,fruit:<span class="string">'apple'</span>&#125;;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// dessert是对象里面的属性，将值赋给dessertnew</span></span><br><span class="line">let &#123;dessert:dessertnew,drink:drinknew,fruit:fruitnew&#125; = breakfast();</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">console.log(dessertnew,drinknew,fruitnew);</span><br><span class="line"><span class="comment">//控制台输出：cake tea apple</span></span><br></pre></td></tr></table></figure>

<h3 id="4-字符串模版"><a href="#4-字符串模版" class="headerlink" title="4.字符串模版"></a>4.字符串模版</h3><h4 id="Template-Strings"><a href="#Template-Strings" class="headerlink" title="Template Strings"></a>Template Strings</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">let dessert=<span class="string">'cake'</span>,drink:<span class="string">'tea'</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 使用  `$&#123;变量名&#125;`</span></span><br><span class="line">let breakfast = `今天的早餐是 $&#123;dessert&#125; 与 $&#123;drink&#125;`</span><br><span class="line"></span><br><span class="line">console.log(breakfast);</span><br><span class="line"><span class="comment">//控制台输出：今天的早餐是cake与tea</span></span><br></pre></td></tr></table></figure>





        </div>

        
            <section class="post-copyright">
                
                    <p class="copyright-item">
                        <span>Author:</span>
                        <span>ZhangPing</span>
                    </p>
                
                
                    <p class="copyright-item">
                        <span>Permalink:</span>
                        <span><a href="https://zhangpingv5.gitee.io/2017/12/12/JavaScript--es6/">https://zhangpingv5.gitee.io/2017/12/12/JavaScript--es6/</a></span>
                    </p>
                
                
                    <p class="copyright-item">
                        <span>License:</span>
                        <span>转载请注明出处</span>
                    </p>
                
                
                     <p class="copyright-item">
                         <span>Slogan:</span>
                         <span>Self-descipline grants me freedom.</span>
                     </p>
                

            </section>
        
        <section class="post-tags">
            <div>
                <span>Tag(s):</span>
                <span class="tag">
                    
                    
                        <a href="/tags/es6/"># es6</a>
                    
                        
                </span>
            </div>
            <div>
                <a href="javascript:window.history.back();">back</a>
                <span>· </span>
                <a href="/">home</a>
            </div>
        </section>
        <section class="post-nav">
            
                <a class="prev" rel="prev" href="/2017/12/22/Ubutu%E4%B8%8B%E7%BC%96%E8%AF%91ijk%20player%E6%95%99%E7%A8%8B/">Ubutu下编译ijk player教程</a>
            
            
            <a class="next" rel="next" href="/2017/11/13/JavaScript--jQuery%E7%9A%84%E4%BA%8B%E4%BB%B6/">JavaScript--jQuery的事件</a>
            
        </section>


    </article>
</div>

        </div>
        <footer id="footer" class="footer">
    <div class="copyright">
        <span>© ZhangPing | Powered by <a href="https://hexo.io" target="_blank">Hexo</a> & <a href="https://github.com/Siricee/hexo-theme-Chic" target="_blank">Chic</a></span>
    </div>
</footer>

    </div>
</body>
</html>
